package com.gking.processHarvest.controller;


import com.gking.processHarvest.common.Res;
import com.gking.processHarvest.entity.Example;
import com.gking.processHarvest.entity.Notes;
import com.gking.processHarvest.service.Example.ExampleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/example")
public class ExampleController {

    @Autowired
    private ExampleService exampleService;


    /**
     * 创建示例
     *
     * @param example 示例信息
     * @return s
     */
    @PostMapping("/create")
    public Res<String> create(@RequestBody Example example) {
        log.warn("传递过来的示例信息为：{}", example);
        return exampleService.create(
                example.getProjectId(), example.getHarvestId(),
                example.getContent(), example.getEditContent());
    }


    /**
     * 更新示例
     *
     * @param example 示例信息
     * @return s
     */
    @PutMapping("/update")
    public Res<String> update(@RequestBody Example example) {
        log.warn("传递过来的更新示例信息为：{}", example);
        return exampleService.update(example.getId(), example.getContent(), example.getEditContent());
    }


    /**
     * 删除示例
     *
     * @param id 示例id
     * @return s
     */
    @DeleteMapping("/delete/{id}")
    public Res<String> delete(@PathVariable String id) {
        log.warn("要修改的示例 id 为：{}", id);
        return exampleService.delete(id);
    }


    /**
     * 获取示例的笔记列表
     *
     * @param exampleId 示例id
     * @return s
     */
    @GetMapping("/noteList/{exampleId}")
    public Res<List<Notes>> getNoteList(@PathVariable String exampleId) {
        log.warn("要获取笔记列表的示例 id 为：{}", exampleId);
        if (exampleId == null || exampleId.equals("")) return Res.error("请将示例id补充完整");
        return exampleService.getNoteList(exampleId);
    }


    /**
     * 获取示例详情
     *
     * @param exampleId 示例id
     * @return s
     */
    @GetMapping("/detail/{exampleId}")
    public Res<Example> getExampleDetail(@PathVariable String exampleId) {
        log.warn("要获取详情的示例 id 为：{}", exampleId);
        if (exampleId == null || exampleId.equals("")) return Res.error("请将示例id补充完整");
        return exampleService.getExampleDetail(exampleId);
    }
}
